package com.pshare.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Pattern;

public class NumberUtil {

	private static final String DEFUALT_FORMAT_PATTERN = "#.##";

	private static final String SHORT_FORMAT_PATTERN = "#.#";

	private static final String DOUBLE_TO_INTEGER_FORMAT_PATTERN = "#";

	private static final String THREE_FORMAT_PATTERN = "#.###";

	public static Double formatDouble(Double num, Integer scale) {
		if (num == null) {
			return null;
		}
		if (scale == null) {
			scale = 2;
		}
		BigDecimal b = new BigDecimal(num);
		return b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	public static Float formatFloat(Float num) {
		if (num == null) {
			return null;
		}
		DecimalFormat f = new DecimalFormat();
		f.applyPattern(DEFUALT_FORMAT_PATTERN);
		return formatStringToFloat(f.format(num));
	}

	public static Float formatFloat(Float num, String pattern) {
		if (num == null) {
			return null;
		}
		DecimalFormat f = new DecimalFormat();
		f.applyPattern(pattern);
		return formatStringToFloat(f.format(num));
	}

	public static String formatFloatToString(Float num) {
		if (num == null) {
			return null;
		}
		DecimalFormat f = new DecimalFormat();
		f.applyPattern(DEFUALT_FORMAT_PATTERN);
		return f.format(num);
	}

	public static String formatDoubleToString(Double num) {
		if (num == null) {
			return null;
		}
		DecimalFormat f = new DecimalFormat();
		f.applyPattern(DEFUALT_FORMAT_PATTERN);
		return f.format(num);
	}

	public static String formatFloatToString(Float num, String pattern) {
		if (num == null || pattern == null) {
			return null;
		}
		DecimalFormat f = new DecimalFormat();
		f.applyPattern(pattern);
		return f.format(num);
	}

	public static Float formatStringToFloat(String str) {
		if (str == null) {
			return null;
		}
		return Float.parseFloat(str.trim());
	}

	/**
	 * 判断一个字符串是否是正整数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isPositiveInteger(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

	/**
	 * 是否是一个正浮点数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isPositiveFloat(String str) {
		return str.matches("\\d+\\.?\\d*");
	}

	/**
	 * 将double四舍五入转换为int
	 * 
	 * @param num
	 * @return
	 */
	public static Integer formatDoubleToInteger(double num) {
		DecimalFormat f = new DecimalFormat();
		f.applyPattern(DOUBLE_TO_INTEGER_FORMAT_PATTERN);
		return Integer.parseInt(f.format(num));
	}

	/**
	 * 2个int相除 四舍五入 保留N位小数
	 * 
	 * @param i 整数1
	 * @param j 整数2
	 * @param scale 保留的小数位数
	 * @return float
	 */
	public static float divideGetFloat(int i, int j, int scale) {
		BigDecimal x = new BigDecimal(i);
		BigDecimal y = new BigDecimal(j);
		return x.divide(y, scale, BigDecimal.ROUND_HALF_UP).floatValue();
	}

	/**
	 * 获得百分比
	 * 
	 * @param i
	 * @return
	 */
	public static float getPercent(double i) {
		BigDecimal d = new BigDecimal(i);
		BigDecimal l = new BigDecimal(100);
		return d.multiply(l).floatValue();
	}

	/**
	 * 2个double相除 四舍五入 保留N位小数
	 * 
	 * @param i
	 * @param j
	 * @param scale 需要保留的位数
	 * @return double
	 */
	public static double divide(double i, double j, int scale) {
		BigDecimal x = new BigDecimal(i);
		BigDecimal y = new BigDecimal(j);
		return x.divide(y, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 * 2个double相乘 四舍五入 保留N位小数
	 * 
	 * @param i
	 * @param j
	 * @param scale 需要保留的位数
	 * @return double
	 */
	public static double multiply(double i, double j, int scale) {
		BigDecimal x = new BigDecimal(i);
		BigDecimal y = new BigDecimal(j);
		return x.multiply(y).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

}
